/*
 * board_config.h
 *
 * Created: 4.11.2013 20:03:24
 *  Author: Ferda
 */ 

#pragma once

//================================================================
#include "digital_pins.h"
#include "util.h"
#include "config.h"
#include <avr/io.h>

//================================================================

extern void board_init();

//=======================================================================================================================
//
// X AXIS FUNC
//
//=======================================================================================================================

#ifdef USE_X_AXIS_DIRECT

	#define X_Enable() WritePin(X_ENABLE_PIN, X_ENABLE_ON)
	#define X_Disable() WritePin(X_ENABLE_ON, !X_ENABLE_ON)
	#define X_StepOn() WritePin(X_STEP_PIN, X_STEP_ON)
	#define X_StepOff() WritePin(X_STEP_PIN, !X_STEP_ON)
	#define X_SetDir(dir) do { if(dir) WritePin(X_DIR_PIN, !INVERT_X_DIR) else WritePin(X_DIR_PIN, X_INVERT_DIR) }while(0)
	#define X_Sleep() WritePin(X_HALF_PIN, X_SLEEP_ON)
	#define X_Wakeup() WritePin(X_HALF_PIN, !X_SLEEP_ON)
	
	#define init_x_axis()	\
		SetPinMode(X_ENABLE_PIN, OUTPUT);	\
		SetPinMode(X_STEP_PIN, OUTPUT);		\
		SetPinMode(X_DIR_PIN, OUTPUT);		\
		SetPinMode(X_HALF_PIN, OUTPUT)
		

#elif defined USE_X_AXIS_FC

	// spolecne nastaveni pro osu X pro oba podporovane typy driveru
	#define X_Enable() WritePin(X_ENABLE_PIN, X_ENABLE_ON)
	#define X_Disable() WritePin(X_ENABLE_ON, !X_ENABLE_ON)
	#define X_StepOn() WritePin(X_STEP_PIN, X_STEP_ON)
	#define X_StepOff() WritePin(X_STEP_PIN, !X_STEP_ON)
	#define X_SetDir(dir) do { if(dir) WritePin(X_DIR_PIN, !INVERT_X_DIR) else WritePin(X_DIR_PIN, X_INVERT_DIR) }while(0)
			
	#define init_x_axis()	\
		SetPinMode(X_ENABLE_PIN, OUTPUT);	\
		SetPinMode(X_STEP_PIN, OUTPUT);		\
		SetPinMode(X_DIR_PIN, OUTPUT);		\
		SetPinMode(X_HALF_PIN, INPUT)
	
#else 

	#define init_x_axis()
	#warning "Axis X is not used."
	
#endif

//=======================================================================================================================
//
// Y AXIS FUNC
//
//=======================================================================================================================

#ifdef USE_Y_AXIS_DIRECT

	// defaultni nastaveni pro osu Y
	#define Y_Enable() WritePin(Y_ENABLE_PIN, Y_ENABLE_ON)
	#define Y_Disable() WritePin(Y_ENABLE_ON, !Y_ENABLE_ON)
	#define Y_StepOn() WritePin(Y_STEP_PIN, Y_STEP_ON)
	#define Y_StepOff() WritePin(Y_STEP_PIN, !Y_STEP_ON)
	#define Y_SetDir(dir) do { if(dir) WritePin(Y_DIR_PIN, !INVERT_Y_DIR) else WritePin(Y_DIR_PIN, Y_INVERT_DIR) }while(0)
	#define Y_Sleep() WritePin(Y_HALF_PIN, Y_SLEEP_ON)
	#define Y_Wakeup() WritePin(Y_HALF_PIN, !Y_SLEEP_ON)
	
	#define init_y_axis()	\
		SetPinMode(Y_ENABLE_PIN, OUTPUT);	\
		SetPinMode(Y_STEP_PIN, OUTPUT);		\
		SetPinMode(Y_DIR_PIN, OUTPUT);		\
		SetPinMode(Y_HALF_PIN, OUTPUT)
	
#elif defined USE_Y_AXIS_FC
	
	// spolecne nastaveni pro osu X pro oba podporovane typy driveru
	#define Y_Enable() WritePin(Y_ENABLE_PIN, Y_ENABLE_ON)
	#define Y_Disable() WritePin(Y_ENABLE_ON, !Y_ENABLE_ON)
	#define Y_StepOn() WritePin(Y_STEP_PIN, Y_STEP_ON)
	#define Y_StepOff() WritePin(Y_STEP_PIN, !Y_STEP_ON)
	#define Y_SetDir(dir) do { if(dir) WritePin(Y_DIR_PIN, !INVERT_Y_DIR) else WritePin(Y_DIR_PIN, Y_INVERT_DIR) }while(0)
	#define Y_Sleep()
	#define Y_Wakeup()
	
	#define init_y_axis()	\
		SetPinMode(Y_ENABLE_PIN, OUTPUT);	\
		SetPinMode(Y_STEP_PIN, OUTPUT);		\
		SetPinMode(Y_DIR_PIN, OUTPUT);		\
		SetPinMode(Y_HALF_PIN, INPUT)
	
#else

	#define init_y_axis()
	#warning "Axis Y is not used"
	
#endif

//=======================================================================================================================
//
// Z AXIS FUNC
//
//=======================================================================================================================

#ifdef USE_Z_AXIS_DIRECT

	// defaultni nastaveni pro osu Y
	#define Z_Enable() WritePin(Z_ENABLE_PIN, Z_ENABLE_ON)
	#define Z_Disable() WritePin(Z_ENABLE_ON, !Z_ENABLE_ON)
	#define Z_StepOn() WritePin(Z_STEP_PIN, Z_STEP_ON)
	#define Z_StepOff() WritePin(Z_STEP_PIN, !Z_STEP_ON)
	#define Z_SetDir(dir) do { if(dir) WritePin(Z_DIR_PIN, !INVERT_Z_DIR) else WritePin(Z_DIR_PIN, Z_INVERT_DIR) }while(0)
	#define Z_Sleep() WritePin(Z_HALF_PIN, Z_SLEEP_ON)
	#define Z_Wakeup() WritePin(Z_HALF_PIN, !Z_SLEEP_ON)

	#define init_z_axis()	\
		SetPinMode(Z_ENABLE_PIN, OUTPUT);	\
		SetPinMode(Z_STEP_PIN, OUTPUT);		\
		SetPinMode(Z_DIR_PIN, OUTPUT);		\
		SetPinMode(Z_HALF_PIN, OUTPUT)

#elif defined USE_Z_AXIS_FC

	// spolecne nastaveni pro osu X pro oba podporovane typy driveru
	#define Z_Enable() WritePin(Z_ENABLE_PIN, Z_ENABLE_ON)
	#define Z_Disable() WritePin(Z_ENABLE_ON, !Z_ENABLE_ON)
	#define Z_StepOn() WritePin(Z_STEP_PIN, Z_STEP_ON)
	#define Z_StepOff() WritePin(Z_STEP_PIN, !Z_STEP_ON)
	#define Z_SetDir(dir) do { if(dir) WritePin(Z_DIR_PIN, !INVERT_Z_DIR) else WritePin(Z_DIR_PIN, Z_INVERT_DIR) }while(0)
	
	#define init_z_axis()	\
		SetPinMode(Z_ENABLE_PIN, OUTPUT);	\
		SetPinMode(Z_STEP_PIN, OUTPUT);		\
		SetPinMode(Z_DIR_PIN, OUTPUT);		\
		SetPinMode(Z_HALF_PIN, INPUT)

#else

	#define init_z_axis()
	#warning "Axis Z is not used"

#endif

//=======================================================================================================================
//
// E1 AXIS FUNC
//
//=======================================================================================================================

#ifdef USE_E1_AXIS_DIRECT

	// defaultni nastaveni pro osu Y
	#define E1_Enable() WritePin(E1_ENABLE_PIN, E1_ENABLE_ON)
	#define E1_Disable() WritePin(E1_ENABLE_ON, !E1_ENABLE_ON)
	#define E1_StepOn() WritePin(E1_STEP_PIN, E1_STEP_ON)
	#define E1_StepOff() WritePin(E1_STEP_PIN, !E1_STEP_ON)
	#define E1_SetDir(dir) do { if(dir) WritePin(E1_DIR_PIN, !INVERT_E1_DIR) else WritePin(E1_DIR_PIN, E1_INVERT_DIR) }while(0)
	#define E_Sleep() WritePin(E_HALF_PIN, E_SLEEP_ON)
	#define E_Wakeup() WritePin(E_HALF_PIN, !E_SLEEP_ON)

	#define init_e1_axis()	\
		SetPinMode(E1_ENABLE_PIN, OUTPUT);	\
		SetPinMode(E1_STEP_PIN, OUTPUT);	\
		SetPinMode(E1_DIR_PIN, OUTPUT);		\
		SetPinMode(E_HALF_PIN, OUTPUT)

#elif defined USE_Z_AXIS_FC

	// spolecne nastaveni pro osu X pro oba podporovane typy driveru
	#define E1_Enable() WritePin(E1_ENABLE_PIN, E1_ENABLE_ON)
	#define E1_Disable() WritePin(E1_ENABLE_ON, !E1_ENABLE_ON)
	#define E1_StepOn() WritePin(E1_STEP_PIN, E1_STEP_ON)
	#define E1_StepOff() WritePin(E1_STEP_PIN, !E1_STEP_ON)
	#define E1_SetDir(dir) do { if(dir) WritePin(E1_DIR_PIN, !INVERT_E1_DIR) else WritePin(E1_DIR_PIN, E1_INVERT_DIR) }while(0)
	

	#define init_e1_axis()	\
		SetPinMode(E1_ENABLE_PIN, OUTPUT);	\
		SetPinMode(E1_STEP_PIN, OUTPUT);	\
		SetPinMode(E1_DIR_PIN, OUTPUT);		\
		SetPinMode(E_HALF_PIN, INPUT)

#else

	#define init_e1_axis()
	#warning "Axis E1 is not used"

#endif

//=======================================================================================================================
//
// E2 AXIS FUNC
//
//=======================================================================================================================

#ifdef USE_E2_AXIS_DIRECT

	// defaultni nastaveni pro osu Y
	#define E2_Enable() WritePin(E2_ENABLE_PIN, E2_ENABLE_ON)
	#define E2_Disable() WritePin(E2_ENABLE_ON, !E2_ENABLE_ON)
	#define E2_StepOn() WritePin(E2_STEP_PIN, E2_STEP_ON)
	#define E2_StepOff() WritePin(E2_STEP_PIN, !E2_STEP_ON)
	#define E2_SetDir(dir) do { if(dir) WritePin(E2_DIR_PIN, !INVERT_E2_DIR) else WritePin(E2_DIR_PIN, E2_INVERT_DIR) }while(0)
	
	#ifndef E_Sleep
		#define E_Sleep() WritePin(E_HALF_PIN, E_SLEEP_ON)
	#endif
	
	#ifndef E_Wakeup
		#define E_Wakeup() WritePin(E_HALF_PIN, !E_SLEEP_ON)
	#endif

	#define init_e2_axis()	\
		SetPinMode(E2_ENABLE_PIN, OUTPUT);	\
		SetPinMode(E2_STEP_PIN, OUTPUT);	\
		SetPinMode(E2_DIR_PIN, OUTPUT);		\
		SetPinMode(E_HALF_PIN, OUTPUT)

#elif defined USE_E2_AXIS_FC

	// spolecne nastaveni pro osu X pro oba podporovane typy driveru
	#define E2_Enable() WritePin(E2_ENABLE_PIN, E2_ENABLE_ON)
	#define E2_Disable() WritePin(E2_ENABLE_ON, !E2_ENABLE_ON)
	#define E2_StepOn() WritePin(E2_STEP_PIN, E2_STEP_ON)
	#define E2_StepOff() WritePin(E2_STEP_PIN, !E2_STEP_ON)
	#define E2_SetDir(dir) do { if(dir) WritePin(E2_DIR_PIN, !INVERT_E2_DIR) else WritePin(E2_DIR_PIN, E2_INVERT_DIR) }while(0)
	
	
	#define init_e2_axis()	\
		SetPinMode(E2_ENABLE_PIN, OUTPUT);	\
		SetPinMode(E2_STEP_PIN, OUTPUT);	\
		SetPinMode(E2_DIR_PIN, OUTPUT);		\
		SetPinMode(E_HALF_PIN, INPUT)

#else

	#define init_e2_axis()
	#warning "Axis E2 is not used"

#endif

